package com.ruoyi.qbms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.qbms.domain.QbmsQuestionChoice;
import com.ruoyi.qbms.service.IQbmsQuestionChoiceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qbms.domain.QbmsChapter;
import com.ruoyi.qbms.service.IQbmsChapterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题库章节Controller
 * 
 * @author zouhuu
 * @date 2022-10-16
 */
@RestController
@RequestMapping("/qbms/qbmsChapter")
public class QbmsChapterController extends BaseController
{
    @Autowired
    private IQbmsChapterService qbmsChapterService;

    @Autowired
    private IQbmsQuestionChoiceService qbmsQuestionChoiceService;

    /**
     * 查询章节选项
     * @param qbmsChapter
     * @return
     */
    @GetMapping("treeselect")
    public AjaxResult treeselect(QbmsChapter qbmsChapter) {
        return AjaxResult.success(qbmsChapterService.selectQbmsChapterTreeselect(qbmsChapter));
    }

    /**
     * 查询题库章节列表
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsChapter:list')")
    @GetMapping("/list")
    public TableDataInfo list(QbmsChapter qbmsChapter)
    {
        startPage();
        List<QbmsChapter> list = qbmsChapterService.selectQbmsChapterList(qbmsChapter);
        return getDataTable(list);
    }

    /**
     * 导出题库章节列表
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsChapter:export')")
    @Log(title = "题库章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QbmsChapter qbmsChapter)
    {
        List<QbmsChapter> list = qbmsChapterService.selectQbmsChapterList(qbmsChapter);
        ExcelUtil<QbmsChapter> util = new ExcelUtil<QbmsChapter>(QbmsChapter.class);
        util.exportExcel(response, list, "题库章节数据");
    }

    /**
     * 获取题库章节详细信息
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsChapter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qbmsChapterService.selectQbmsChapterById(id));
    }

    /**
     * 新增题库章节
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsChapter:add')")
    @Log(title = "题库章节", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QbmsChapter qbmsChapter)
    {
        return toAjax(qbmsChapterService.insertQbmsChapter(qbmsChapter));
    }

    /**
     * 修改题库章节
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsChapter:edit')")
    @Log(title = "题库章节", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QbmsChapter qbmsChapter)
    {
        return toAjax(qbmsChapterService.updateQbmsChapter(qbmsChapter));
    }

    /**
     * 删除题库章节
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsChapter:remove')")
    @Log(title = "题库章节", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            QbmsQuestionChoice qbmsQuestionChoice = new QbmsQuestionChoice();
            qbmsQuestionChoice.setChapterId(id);
            List<QbmsQuestionChoice> qbmsQuestionChoices = qbmsQuestionChoiceService.selectQbmsQuestionChoiceList(qbmsQuestionChoice);
            if (qbmsQuestionChoices.size() > 0) {
                return  AjaxResult.error("该章节下存在题目，不能删除。");
            }
        }
        return toAjax(qbmsChapterService.deleteQbmsChapterByIds(ids));
    }
}
